using System;
using System.Collections;
using System.Collections.Generic;

namespace Prologis.ShifterNT {


    public class GenericList<T> : IEnumerator where T : ICloneable, IKey, new() {

        int enumIndex = -1;
        IDictionary<string, T> hashList = new Dictionary<string, T>();
        IList<T> arrayList = new List<T>();


        /// <summary>
        /// Initializes a new instance of the <see cref="T:GenericList&lt;T&gt;"/> class.
        /// </summary>
        public GenericList() { }


        /// <summary>
        /// Determines whether the specified key contains key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>
        /// 	<c>true</c> if the specified key contains key; otherwise, <c>false</c>.
        /// </returns>
        public bool ContainsKey(string key) {
            return hashList.ContainsKey(key);
        }


        /// <summary>
        /// 
        /// </summary>
        /// 
        public int Count {
            get { return arrayList.Count; }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// 
        public IEnumerator GetEnumerator() {
            enumIndex = -1;
            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        /// 
        public int Add(T item) {
            hashList.Add(item.Key, item);
            arrayList.Add(item);
            return arrayList.Count;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        /// 
        public T this[int index] {
            get { return arrayList[index]; }
            set { arrayList[index] = value; }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        /// 
        public T this[string name] {
            get {
                if (hashList.ContainsKey(name)) {
                    return hashList[name];
                }
                throw new Exception("FieldList does not contain the key [" + name + "] !");
            }
            set { hashList[name] = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// 
        public void Reset() {
            enumIndex = -1;
        }


        /// <summary>
        /// 
        /// </summary>
        /// 
        public object Current {
            get {
                return arrayList[enumIndex];
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// 
        public bool MoveNext() {
            return ++enumIndex < arrayList.Count;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="RT"></typeparam>
        /// <returns></returns>
        /// 
        public object Clone() {
            GenericList<T> list = new GenericList<T>();
            for (int i = 0; i < arrayList.Count; i++) {
                T item = (T)arrayList[i].Clone();
                list.Add(item);
            }
            return list;
        }


    }
}
